import unittest

import utils
from api.login import LoginApi
import requests

from api.recharge import RechargeApi
from api.tender import TenderApi
from api.trust_account import TrustAccountApi


class TestTenderFlow(unittest.TestCase):
    phone = "13020210002"

    @classmethod
    def setUpClass(cls) -> None:
        cls.login_api = LoginApi()
        cls.trust_account = TrustAccountApi()
        cls.recharge_api = RechargeApi()
        cls.tender_api = TenderApi()
        cls.session = requests.Session()

        # 数据清理
        sql1 = "DELETE FROM mb_member WHERE phone='13020210002';"
        sql2 = "DELETE FROM mb_member_info WHERE member_name='13020210002';"
        sql3 = "DELETE FROM mb_member_register_log WHERE member_name='13020210002';"
        utils.DBUtil.execute(sql1, sql2, sql3)

    @classmethod
    def tearDownClass(cls) -> None:
        cls.session.close()

    # 获取图片验证码成功
    def test01_get_img_verify_code(self):
        # 测试数据
        r = 0.123123123123

        # 调用接口
        response = self.login_api.get_img_verify_code(self.session, r)

        # 断言
        self.assertEqual(200, response.status_code)

    # 获取短信验证码成功(正确的手机号,验证码,类型)
    def test02_get_sms_code(self):
        # 测试数据
        phone = TestTenderFlow.phone
        imgVerifyCode = "8888"

        # 调用接口
        # 获取短信验证码
        r = self.login_api.get_sms_code(self.session, phone, imgVerifyCode)
        print("get_sms_code data=", r.json())

        # 断言
        utils.common_assert(self, r, 200, 200, "短信发送成功")

    # 注册成功-必填参数
    def test03_reg_success(self):
        # 测试数据
        phone = TestTenderFlow.phone
        password = "test123"
        verifycode = "8888"
        phone_code = "666666"
        dy_server = "on"

        # 调用接口
        r3 = self.login_api.reg(self.session, phone, password, verifycode, phone_code, dy_server)
        print("reg data=", r3.json())

        # 断言
        utils.common_assert(self, r3, 200, 200, "注册成功")

    # 登录成功-用户名和密码正确
    def test04_login_success(self):
        # 测试数据
        keywords = TestTenderFlow.phone
        password = "test123"

        # 调用接口
        r = self.login_api.login(self.session, keywords, password)
        print("login data=", r.json())

        # 断言
        utils.common_assert(self, r, 200, 200, "登录成功")

    # 实名认证成功（正确的姓名和身份证号）
    def test05_approve_realname_success(self):
        # 测试数据
        realname = "小一"
        card_id = "110101199003072447"

        # 调用接口
        r = self.trust_account.approve_realname(self.session, realname, card_id)
        print("approve realname data=", r.json())

        # 断言
        utils.common_assert(self, r, 200, 200, "提交成功")

    # 开户成功
    def test06_trust_register_success(self):
        # 调用接口
        r = self.trust_account.trust_register(self.session)
        print("trust_register text=", r.text)
        print("trust_register data=", r.json())

        # 断言
        utils.common_assert(self, r, 200, 200, None)

        # 提取表单数据
        html = r.json().get("description").get("form")
        print("html==", html)

        action, form_data = utils.parse_html(html)

        # 调用第三方开户接口
        response = requests.post(action, data=form_data)
        self.assertEqual(200, response.status_code)
        print("第三方开户接口返回结果：", response.text)

    # 充值成功
    def test07_recharge_success(self):
        # 测试数据
        amount = 300000
        valicode = 8888

        # 调用接口
        r1 = self.recharge_api.get_recharge_img_verify(self.session)
        self.assertEqual(200, r1.status_code)

        # 充值
        r2 = self.recharge_api.recharge(self.session, amount, valicode)
        print("recharge data=", r2.json())
        utils.common_assert(self, r2, 200, 200, None)

        # 提取表单数据
        html = r2.json().get("description").get("form")
        print("html====", html)
        action, form_data = utils.parse_html(html)

        # 调用第三方充值接口
        r3 = requests.post(action, data=form_data)
        print("第三方充值接口返回数据：", r3.text)
        self.assertEqual(200, r3.status_code)

    # 调用第三方投资接口
    def test08_tender_three(self):
        # 测试数据
        id = 2059
        amount = 100

        # 调用接口
        r = self.tender_api.tender(self.session, id, amount)
        print("tender data=", r.json())

        # 断言
        utils.common_assert(self, r, 200, 200, None)

        # 提取form数据
        html = r.json().get("description").get("form")
        action, form_data = utils.parse_html(html)

        # 调用第三方接口
        r2 = requests.post(action, data=form_data)
        print("第三方投资接口返回数据：", r2.text)
        self.assertEqual(200, r2.status_code)
        self.assertIn("OK", r2.text)
